<?php
class BaseTaxonomy extends CInputWidget
{
    public $taxonomy;//taxonomy name
    public $module;
    public $objectId;
    
    public $state=Vocabulary::STATE_ACTIVE;
    
    private $_data;
    private $_selected;
        
    public function getData()
    {
        if ($this->_data === null)
        {
            $this->_data = array();
            //find vocabulary
            $criteria = new CDbCriteria();
            $criteria->compare('module', $this->module);
            $criteria->compare('state', $this->state);
            if (is_numeric($this->taxonomy))
                $criteria->compare('id',$this->taxonomy);
            else
                $criteria->compare('alias',$this->taxonomy);
            $vocabulary = Vocabulary::model()->find($criteria);
            if (is_object($vocabulary))
            {
                //find terms recursive
                $root = $this->findTermRoot($vocabulary->id);
                if (count($root))
                {
                    foreach ($root as $parent)
                    {
                        $this->_data[] = $this->findTermsRecursive($parent);
                    }
                }
            }
            
        }
        return $this->_data;
    }
    
    public function setData($data)
    {
        $this->_data = CPropertyValue::ensureArray($data);
    }
    
    protected function findTermRoot($vocId)
    {
        $criteria = new CDbCriteria();
        $criteria->compare('v_id', $vocId);
        $criteria->compare('state', $this->state);
        $criteria->addCondition('id NOT IN (SELECT term_id FROM '.SITE_TABLE_PREFIX.'taxonomy_term_hierarchy)');
        $criteria->order = 'ordering';
        return Term::model()->findAll($criteria);
    }
    
    /**
    * find terms recursive
    * 
    * @param Term $parent
    */
    protected function findTermsRecursive($parent, $level=0, $path='/')
    {
        $data = $parent->attributes;
        $data['level'] = $level++;
        $children = $parent->children(array('order'=>'ordering'));
        if (is_array($children) && count($children))
        {
            $data['children'] = array();
            foreach ($children as $term)
            {
                 $data['children'][] = $this->findTermsRecursive($term, $level);
            }
        }
        return $data;
    }
    
    public function getSelected()
    {
        if ($this->_selected === null)
        {
            $criteria = new CDbCriteria();
            $criteria->join = "INNER JOIN ".SITE_TABLE_PREFIX."taxonomy_term tt ON tt.id = t.term_id"
                            ."\n INNER JOIN ".SITE_TABLE_PREFIX."taxonomy_vocabulary tv ON tv.id = tt.v_id";
            $criteria->compare('t.object_id', (int) $this->objectId);
            if (is_numeric($this->taxonomy))
                $criteria->compare('tv.id', $this->taxonomy);
            else
                $criteria->compare('tv.alias', $this->taxonomy);
            $criteria->compare('tv.module', $this->module);
            $criteria->compare('tv.state', $this->state);
            $models = TermRelation::model()->findAll($criteria);
	    
            $this->_selected = array();
            if (count($models))
            {
                $this->_selected = CHtml::listData($models, 'term_id', 'term_id');
            }
        }
        return $this->_selected;
    }
    
    public function setSelected($data)
    {
        $this->_selected = $data;
    }
}
